//
//  SZLRegisterViewController.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/7/19.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SZLRegisterViewController.h"
#import "MBProgressHUD+MJ.h"
#import "LoginRegisterService.h"

@interface SZLRegisterViewController ()
//注册 修改密码类型
@property (nonatomic,assign) CodeType codeType;
@property (weak, nonatomic) IBOutlet UITextField *codeText;
@property (weak, nonatomic) IBOutlet UITextField *pswText;
@property (weak, nonatomic) IBOutlet UITextField *pswAgainText;
@property (weak, nonatomic) IBOutlet UIButton *getCodeBtn;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;

@property (nonatomic,strong) NSTimer *timer;

@end

@implementation SZLRegisterViewController

- (instancetype)initWithCodeType:(CodeType)type
{
    if (self = [super init]) {
        self.codeType = type;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.getCodeBtn.enabled = NO;
    if (self.codeType == CodeTypeRegister) {
        self.navigationItem.title = @"新用户注册";
    }else{
        self.navigationItem.title = @"找回密码";
        [self.nextBtn setTitle:@"确定" forState:UIControlStateNormal];
    }
    [self.timer fire];
}
- (void)timeDown
{
    NSInteger count = [self.getCodeBtn.titleLabel.text integerValue]-1;
    if (count<0) {
        [self endTimer];
        self.getCodeBtn.enabled = YES;
        [self.getCodeBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        return;
    }
    [self.getCodeBtn setTitle:[NSString stringWithFormat:@"%zd",count] forState:UIControlStateNormal];
}
- (IBAction)btnsClick:(UIButton *)btn
{
    if (btn.tag == 0) {
        self.getCodeBtn.enabled = NO;
        [self.getCodeBtn setTitle:@"60" forState:UIControlStateNormal];
        [self.timer fire];
        [self getCodeAgain];
    }else{
        [self registerClick];
    }
}
- (void)registerClick
{
    if (self.codeText.text.length<1) {
        [MBProgressHUD showError:@"请输入验证码" toView:self.view];
        return;
    }
    if (self.pswText.text.length<1) {
        [MBProgressHUD showError:@"请输入密码" toView:self.view];
        return;
    }
    if (![self.pswText.text isEqualToString:self.pswAgainText.text]) {
        [MBProgressHUD showError:@"两次密码不一致" toView:self.view];
        return;
    }
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"mobilePhone"] = self.tell;
    parame[@"smsCode"] = self.codeText.text;
    if (self.codeType == CodeTypeRegister) {
        parame[@"loginPwd"] = self.pswText.text;
        [self registerAction:parame];
    }else{
        parame[@"pwd"] = self.pswText.text;
        [self forgetPswAction:parame];
    }
}
- (void)forgetPswAction:(NSDictionary *)parame
{
    [self showWaiter];
    [LoginRegisterService forgetWithParame:parame response:^(BOOL success, NSString *msg) {
        [self hideWaiter];
        if (success) {
            [MBProgressHUD showSuccess:@"密码修改成功" toView:self.view];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popToRootViewControllerAnimated:YES];
            });
        }else{
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (void)registerAction:(NSDictionary *)parame
{
    [self showWaiter];
    [LoginRegisterService registerWithParame:parame response:^(BOOL success, NSString *msg) {
        [self hideWaiter];
        if (success) {
            [MBProgressHUD showSuccess:@"注册成功" toView:self.view];
            [[NSNotificationCenter defaultCenter]postNotificationName:UserLogined object:nil];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popToRootViewControllerAnimated:YES];
            });
        }else{
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (void)getCodeAgain
{
    [LoginRegisterService getVerfyCodeWithParame:@{@"mobilePhone":self.tell} response:^(BOOL success, NSString *msg) {
        [self hideWaiter];
        if (success) {
            [MBProgressHUD showSuccess:@"验证码获取成功" toView:self.view];
        }else{
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (void)endTimer
{
    [self.timer invalidate];
    self.timer = nil;
}
- (NSTimer *)timer
{
    if (!_timer) {
        _timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timeDown) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];
    }
    return _timer;
}
@end
